package com.site.wisdom_construction.controller.login;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.site.wisdom_construction.entity.userAuthorityModule.User;
import com.site.wisdom_construction.responseEntity.LoginResult;
import com.site.wisdom_construction.responseEntity.UserLoginResult;
import com.site.wisdom_construction.service.userAuthorityModule.UserService;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2021/12/22
 * @Description
 */
@Api( tags = "登录接口")
@RestController
@RequestMapping("Login")
@CrossOrigin
public class LoginController {

    @Autowired
    private UserService userService;

    @NoToken
    @ApiOperation(value = "登录接口", notes = "测试数据:")
    @PostMapping("/backgroundLogin")
    public LoginResult insertCompany(@RequestBody Map map) {

        LoginResult loginResult = new LoginResult();
        //根据用户名查询用户数据
        User user = userService.findUserByContactsPhone(map);

        //验证用户是否存在
        if (user != null) {
            //封装返回实体类
            UserLoginResult userLoginResult = new UserLoginResult();
            userLoginResult.setId(user.getId());
            userLoginResult.setAccount(user.getAccount());
            userLoginResult.setContactsName(user.getContactsName());
            userLoginResult.setContactsPhone(user.getContactsPhone());
            userLoginResult.setProjectId(user.getProjectId());
            userLoginResult.setProjectEndTime(user.getProjectEndTime());
            userLoginResult.setProjectStartTime(user.getProjectStartTime());
            userLoginResult.setFatherId(user.getFatherId());
            userLoginResult.setIsparent(user.getIsparent());
            userLoginResult.setLeave(user.getLeave());
            userLoginResult.setStatus(user.getStatus());
            userLoginResult.setProjectName(user.getProjectName());

            //验证账号是否可用(到期)
            if (user.getStatus().equals("1")) {
                //验证密码是否正确
                if ((map.get("password").equals(user.getPassword()))) {
                    String token = JWT.create().withAudience(user.getId() + "")
                            .sign(Algorithm.HMAC256(user.getPassword()));
                    loginResult.setCode(200);
                    loginResult.setMessage("登录成功!");
                    loginResult.setResult(userLoginResult);
                    loginResult.setToken(token);
                } else {
                    loginResult.setCode(20002);
                    loginResult.setMessage("密码错误,请重新输入密码");
                }

            } else {
                loginResult.setCode(20001);
                loginResult.setMessage("账号已过期,请通知管理员");
            }

        } else {
            loginResult.setCode(20000);
            loginResult.setMessage("账号不存在");
        }
        return loginResult;
    }
}
